package vn.vfriends.vcms.admin.bean;

import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vn.vfriends.vcms.admin.model.Item;
import vn.vfriends.vcms.admin.model.Status;
import vn.vfriends.vcms.admin.service.ItemService;

/**
 *
 * @author tuan@vfriends.vn
 */
@Named
@ConversationScoped
public class ItemBean implements Serializable {
    private final Logger logger = LoggerFactory.getLogger(ItemBean.class);
    
    @Inject private ItemService itemService;
    @Inject private Conversation conversation;
    private Item[] selectedItems;
    private Item item;
    private List<Item> itemList;
    
    @PostConstruct
    public void init(){
        itemList = itemService.findItems();
    }

    public List<Item> getItemList() {
        return itemList;
    }
    
    public Status[] getStatusList() {
        return Status.values();
    }

    public Item[] getSelectedItems() {
        return selectedItems;
    }

    public void setSelectedItems(Item[] selectedItems) {
        this.selectedItems = selectedItems;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    public Conversation getConversation() {
        return conversation;
    }
    
    public void preNewAction() {
        item = new Item();
        
        if (conversation.isTransient()) {
            conversation.begin();
        }
    }
    
    public String preViewAction() {
        String outcome = "";
        FacesContext context = FacesContext.getCurrentInstance();
        
        try {
            if (conversation.isTransient()) {
                conversation.begin();
            }
            
            if(this.item != null) {
                outcome = "view?faces-redirect=true";
            }
            else if (this.selectedItems == null || this.selectedItems.length == 0) {
                if (RequestContext.getCurrentInstance() != null) {
                    RequestContext.getCurrentInstance().addCallbackParam("invalid", true);
                }
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Hãy chọn một bài viết", ""));
            } else if (this.selectedItems.length > 1) {
                if (RequestContext.getCurrentInstance() != null) {
                    RequestContext.getCurrentInstance().addCallbackParam("invalid", true);
                }
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Hãy chọn chỉ một bài viết", ""));
            } else {
                this.item = this.selectedItems[0];
                outcome = "view?faces-redirect=true";
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), ""));
        }
        return outcome;
    }
    
    public String preEditAction() {
        String outcome = null;
        FacesContext context = FacesContext.getCurrentInstance();
        try {
            if (conversation.isTransient()) {
                conversation.begin();
            }
            
            if(this.item != null){
                outcome = "edit?faces-redirect=true";
            } else if (this.selectedItems == null || this.selectedItems.length == 0) {
                if (RequestContext.getCurrentInstance() != null) {
                    RequestContext.getCurrentInstance().addCallbackParam("invalid", true);
                }
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Hãy chọn một bài viết", ""));
            } else if (this.selectedItems.length > 1) {
                if (RequestContext.getCurrentInstance() != null) {
                    RequestContext.getCurrentInstance().addCallbackParam("invalid", true);
                }
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Hãy chọn chỉ một bài viết", ""));
            } else {
                this.item = this.selectedItems[0];
                outcome = "edit?faces-redirect=true";
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), ""));

        }
        return outcome;
    }
    
    public void preDeleteAction() {
        FacesContext context = FacesContext.getCurrentInstance();
        try {
            if (conversation.isTransient()) {
                conversation.begin();
            }
            if ((this.item == null) && (this.selectedItems == null || this.selectedItems.length == 0)) {
                if (RequestContext.getCurrentInstance() != null) {
                    RequestContext.getCurrentInstance().addCallbackParam("invalid", true);
                }
                RequestContext.getCurrentInstance().addCallbackParam("invalid", true);
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Hãy chọn bài viết", ""));
            }
        } catch (Exception e) {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), ""));
        }
    }
    
    public String saveAction() {
        try {
            this.itemService.createItem(item);
            return "view?faces-redirect=true";
        } catch (Exception ex) {
            this.logger.error(ex.getMessage(), ex);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
            return null;
        }
    }
    
    public String updateAction() {
        try {
            this.itemService.editItem(item);
            return "view?faces-redirect=true";
        } catch (Exception ex) {
            this.logger.error(ex.getMessage(), ex);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
            return null;
        }
    }

    public String cancelAction() {
        conversation.end();
        return "list?faces-redirect=true";
    }
    
    public void deleteAction() {
        try {
            if (this.item != null) {
                this.itemService.destroyItem(this.item.getId());
            } else {
                for (Item selectedItem : selectedItems) {
                    this.itemService.destroyItem(selectedItem.getId());
                }
            }
            if(!conversation.isTransient()) {
                conversation.end();
            }
            FacesContext.getCurrentInstance().getExternalContext().redirect("list.jsf");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), ""));
        }
    }
}
